package com.leizi.igym.controller.admin;

import cn.hutool.core.collection.CollectionUtil;
import com.leizi.igym.dto.CategoryAddDTO;
import com.leizi.igym.dto.CategoryDTO;
import com.leizi.igym.dto.CategoryPageQueryDTO;
import com.leizi.igym.enums.ReturnCodeEnum;
import com.leizi.igym.exception.BusinessException;
import com.leizi.igym.resp.PageResult;
import com.leizi.igym.resp.Result;
import com.leizi.igym.service.CategoryService;
import com.leizi.igym.utils.ThrowUtils;
import com.leizi.igym.vo.CategoryCascaderVO;
import com.leizi.igym.vo.CategoryVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author: leizi0110
 * @CreateTime: 2024-04-08  09:15
 * @Description:
 */

@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Tag(name = "管理端-分类相关接口")
@Slf4j
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @Operation(summary = "管理端：新增分类")
    @PostMapping("parent")
    public Result addParentCategory(@RequestBody CategoryAddDTO dto) {
        log.info("管理端正在新增分类，参数为：{}", dto);
        if (dto == null) {
            throw new BusinessException(ReturnCodeEnum.PARAMS_ERROR);
        }

        boolean result = categoryService.saveParentCategoryWithChild(dto);
        if (!result) {
            throw new BusinessException(ReturnCodeEnum.OPERATION_ERROR, "新增失败！");
        }
        return Result.ok(result);
    }

    @Operation(summary = "管理端：删除分类")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        log.info("管理端正在删除分类，参数为：{}", id);
        if (id == null || id <= 0) {
            throw new BusinessException(ReturnCodeEnum.PARAMS_ERROR);
        }

        return Result.ok(categoryService.removeCategory(id));
    }

    @Operation(summary = "管理端：修改分类")
    @PutMapping("{id}")
    public Result updateCategory(@PathVariable("id") Long id,
                                 @RequestBody CategoryDTO dto) {
        log.info("管理端修改分类，参数为：{}", dto);
        ThrowUtils.throwIf(Objects.isNull(dto) ||
                        id == null || id <= 0 || !dto.getId().equals(id)
                , ReturnCodeEnum.PARAMS_ERROR);
        boolean result = categoryService.updateCategory(dto);
        if (!result) {
            throw new BusinessException(ReturnCodeEnum.OPERATION_ERROR, "删除失败！");
        }
        return Result.ok(result);
    }

    @Operation(summary = "管理端：查询所有分类")
    @GetMapping
    public Result<List<CategoryVO>> getCategory() {
        log.info("管理端查询所有分类");
        return Result.ok(categoryService.getCategoryWithChild());
    }

    @Operation(summary = "管理端：根据id查询分类")
    @GetMapping("{id}")
    public Result<CategoryVO> getCategoryById(@PathVariable("id") Long id) {
        log.info("管理端根据id查询分类");
        CategoryVO categoryVO = categoryService.getByIdWithParent(id);
        return Result.ok(categoryVO);
    }

    @Operation(summary = "管理端：查询所有分类以级联形式返回")
    @GetMapping("cascader")
    public Result<List<CategoryCascaderVO>> getCategoryCascaderVO() {
        log.info("管理端查询所有分类以级联形式返回");
        return Result.ok(categoryService.getCategoryCascaderVO());
    }

    @Operation(summary = "管理端：分页查询分类")
    @GetMapping("/{current}/{pageSize}")
    public Result<PageResult> getCategoryPage(@PathVariable("current") Integer current,
                                              @PathVariable("pageSize") Integer pageSize,
                                              CategoryPageQueryDTO dto) {
        log.info("管理端分页查询分类，当前页数为：{}，分页大小：{}，参数为：{}", current, pageSize, dto);
        dto.setCurrent(current);
        dto.setPageSize(pageSize);
        PageResult pageResult = categoryService.getPageWithChildren(dto);

        return Result.ok(pageResult);
    }

    @Operation(summary = "管理端：获取顶级分类")
    @GetMapping("/top")
    public Result getTopCategory() {
        log.info("管理端获取顶级分类");

        List<CategoryVO> topCategory = categoryService.getTopCategory();

        return Result.ok(topCategory);
    }

    @Operation(summary = "管理端：批量删除分类")
    @DeleteMapping("batch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        log.info("管理端批量删除分类，id为：{}", ids);
        ThrowUtils.throwIf(CollectionUtil.isEmpty(ids), ReturnCodeEnum.PARAMS_ERROR);
        boolean result = categoryService.removeByIds(ids);
        if (!result) {
            throw new BusinessException(ReturnCodeEnum.SYSTEM_ERROR, "删除器械信息失败！");
        }
        return Result.ok(result);
    }

}
